// ALGORITHM.h : Declaration of the ALGORITHM

#pragma once
#include "resource.h"       // main symbols
#include "DmhMemory.h"
#include "..\..\oledbdm.h"
#include "DmhLocalization.h"
#include "ParamHandler.h"
#include "lrpmodel.h"

#include "Plugin.h"


// ALGORITHM

class ATL_NO_VTABLE ALGORITHM : 
	public CComObjectRootEx<CComMultiThreadModel>,
	public CComCoClass<ALGORITHM, &CLSID_ALGORITHM>,
	public ISupportErrorInfo,
	public IDMAlgorithm,
	public IDMPersist,
	public DMHALLOC
{
public:
	ALGORITHM()
	{
		_bDisplayCorrelation = false;
		_dblMinDepScore      = 0.0;
	}

DECLARE_REGISTRY_RESOURCEID(IDR_ALGORITHM)
DECLARE_STATIC_PARAMETER_HANDLING()


BEGIN_COM_MAP(ALGORITHM)
	COM_INTERFACE_ENTRY(IDMAlgorithm)
	COM_INTERFACE_ENTRY(ISupportErrorInfo)
	COM_INTERFACE_ENTRY(IDMPersist)
END_COM_MAP()

// ISupportsErrorInfo
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}
	
	void FinalRelease() 
	{
	}

public:

	CComPtr<IDMAttributeSet> _spidmattributeset;
	bool					 _bDisplayCorrelation;
	double					 _dblMinDepScore;
	LRPMODEL				 _lrpmodel;

// IDMAlgorithm

    STDMETHOD (Initialize)( 
        /* [in] */ IDMContextServices *in_pExeContext,
        /* [in] */ IDMAttributeSet *in_pAttributeSet,
        /* [in] */ IDMMarginalStat *in_pMarginalStat);
    
    STDMETHOD (InsertCases)( 
        /* [in] */ IDMContextServices *in_pExeContext,
        /* [in] */ IDMPushCaseSet *in_pCaseSet,
        /* [in] */ ULONG in_ulNumParameters,
        /* [in] */ WCHAR *in_rgParameterNames[  ],
        /* [in] */ DMVariantPtr in_rgParameterValues[  ]);
    
    STDMETHOD (Predict)( 
        /* [in] */ IDMContextServices *in_pContext,
        /* [in] */ DM_PREDICTION_FLAGS in_Flags,
        /* [in] */ IDMAttributeGroup *in_pPredAttGroup,
        /* [in] */ DM_CaseID in_CaseID,
        /* [in] */ ULONG in_ulCaseValues,
        /* [in] */ DM_ATTRIBUTE_VALUE *in_rgValues,
        /* [in] */ ULONG in_ulMaxPredictions,
        /* [in] */ ULONG in_ulMaxStates,
        /* [out] */ DM_ATTRIBUTE_STAT **io_prgPredictions,
        /* [out] */ ULONG *out_pulPredictions);
    
    STDMETHOD (GetNodeIDsForCase)( 
        /* [in] */ IDMContextServices *in_pContext,
        /* [in] */ DM_NODE_FLAGS in_Flags,
        /* [in] */ DM_CaseID in_CaseID,
        /* [in] */ ULONG in_ulCaseValues,
        /* [in] */ DM_ATTRIBUTE_VALUE *in_rgValues,
        /* [in] */ ULONG in_ulMaxNodeIDs,
        /* [out] */ DMStringPtr **io_prgNodeIDs,
        /* [out] */ ULONG *out_pulNodeIDs);
	  
    STDMETHOD (GetNavigator)( 
        /* [in] */ IDMContextServices *in_pContext,
        /* [in] */ BOOL in_fForDimensionContent,
        /* [out] */ IDMAlgorithmNavigation **out_ppDAGNav);
    
    STDMETHOD (GetSampleCaseSet)( 
        /* [in] */ IDMContextServices *in_pContext,
        /* [in] */ DMString *in_pstrNodeID,
        /* [out] */ IDMPullCaseSet **out_ppCaseSet);


	STDMETHOD (GetTrainingParameterActualValue)( 
		/* [in] */ IDMContextServices *in_pContext,
		/* [in] */ ULONG in_iParameter,
		/* [in] */ DMVariantPtr io_pParameterValue);

	STDMETHOD (HasFeatureSelection)( 
		/* [in] */ IDMContextServices *in_pContext,
		/* [out] */ BOOL *out_pHasFS);

	STDMETHOD (GetFeatureSelectedAttributes)( 
		/* [in] */ IDMContextServices *in_pContext,
		/* [out] */ ULONG *out_pCount,
		/* [out] */ DM_Attribute **out_prgFeatureSelectedAttributes,
		/* [out] */ DM_ATTRIBUTE_FLAGS **out_prgFSAttributeFlags);

	STDMETHOD (GetAttributeFeatureSelectionFlags)( 
		/* [in] */ IDMContextServices *in_pContext,
		/* [in] */ DM_Attribute in_nAttr,
		/* [out] */ DM_ATTRIBUTE_FLAGS *out_pAttFlags);

	STDMETHOD (GetProcessingMemoryEstimate)( 
		/* [in] */ IDMContextServices *in_pContext,
		/* [out] */ LONG *out_pEstimate);

	virtual void STDMETHODCALLTYPE UpdateMemoryEstimate( 
		/* [in] */ LONG in_lEstimateChange);

	virtual LONG STDMETHODCALLTYPE ReservedMemoryEstimate( void );

	STDMETHOD (RenderPMMLContent)( 
		/* [in] */ IDMContextServices *in_pContext,
		/* [in] */ ISAXContentHandler *in_pXMLWriter);

	STDMETHOD (PreInitializeForPMML21Parsing)( 
		/* [in] */ IDMContextServices *in_pContext,
		/* [in] */ IDMAttributeSet *in_pAttributeSet);

	STDMETHOD (GetPMMLAlgorithmSAXHandler)( 
		/* [in] */ IDMContextServices *in_pContext,
		/* [out] */ ISAXContentHandler **out_ppSAXHandler);

// IDMPersist interface
	
	STDMETHOD	(Load)(	
					IDMContextServices*		in_pContext,
					IDMPersistenceReader*	in_pReader	);

	STDMETHOD	(Save)(	
					IDMContextServices*		in_pContext,
					IDMPersistenceWriter*	in_pWriter	);

// IDMAlgorithmMetadata
	
    static HRESULT GetServiceType( 
        /* [in]  */	IDMContextServices* in_pContext,
		/* [out] */ ULONG *out_pServiceTypeID);
    
    static HRESULT GetServiceName( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ DMString *io_pstrServiceName);
    
    static HRESULT GetDisplayName( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ DMString *io_pstrDisplayName);
    
    static HRESULT GetServiceGuid( 
		/* [in]  */	IDMContextServices* in_pContext,    
		/* [out] */ GUID *out_pguidServiceGUID);
    
    static HRESULT GetDescription( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ DMString *io_pstrDescription);
    
    static HRESULT GetPredictionLimit( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ LONG *out_pPredictionLimit);
    
    static HRESULT GetSupDistributionFlags( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ ULONG **out_prgFlags,
        /* [out] */ ULONG *out_cFlags);
    
    static HRESULT GetSupInputContentTypes( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ ULONG **out_prgTypes,
        /* [out] */ ULONG *out_cTypes);
    
    static HRESULT GetSupPredictContentTypes( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ ULONG **out_prgTypes,
        /* [out] */ ULONG *out_cTypes);
    
    static HRESULT GetSupModelingFlags( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ DM_MODELING_FLAG **out_prgFlags,
        /* [out] */ ULONG *out_pcFlags);
    
    static HRESULT GetModelingFlagName( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [in]  */ DM_MODELING_FLAG in_Flag,
        /* [out] */ DMString *io_pstrFlagName);
    
    static HRESULT GetTrainingComplexity( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ LONG *out_pTrainingComplexity);
    
    static HRESULT GetPredictionComplexity( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ LONG *out_pPredictionComplexity);
    
    static HRESULT GetExpectedQuality( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ LONG *out_pExpectedQuality);
    
    static HRESULT GetScaling( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ LONG *out_pScaling);
    
    static HRESULT GetAllowIncrementalInsert( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ BOOL *out_pfIncrementalInsert);
   
    static HRESULT GetAllowDuplicateKey( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ BOOL *out_pfDuplicateKey);
    
    static HRESULT GetControl( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ LONG *out_pControl);
    
    static HRESULT GetViewerType( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ DMString *io_pstrViewerType);
    
    static HRESULT GetSupportsDMDimensions( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ BOOL *out_pfSupportsDMDimensions);
    
    static HRESULT GetSupportsDrillthrough( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ BOOL *out_pfSupportsDrillthrough,
		/* [out] */ BOOL *out_pfMustIncludeChildren);

    static HRESULT GetSupportedFunctions( 
        /* [in] */ IDMContextServices *in_pContext,
        /* [out] */ DM_SUPPORTED_FUNCTION **out_prgFuncs,
        /* [out] */ ULONG *out_cFuncs);
    
    static HRESULT GetNumParameters( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ ULONG *out_pcParameters);
    
    static HRESULT GetParameterName( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [in] */ ULONG in_iParameter,
        /* [out] */ DMString *io_pstrParameterName);
    
    static HRESULT GetParameterType( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [in] */ ULONG in_iParameter,
        /* [out] */ DMString *io_pstrParameterType);
    
    static HRESULT GetParameterIsRequired( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [in] */ ULONG in_iParameter,
        /* [out] */ BOOL *out_pfIsRequired);

    static HRESULT GetParameterIsExposed( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [in] */ ULONG in_iParameter,
        /* [out] */ BOOL *out_pfIsExposed);
    
    static HRESULT GetParameterFlags( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [in] */ ULONG in_iParameter,
        /* [out] */ ULONG *out_pulParameterFlags);
    
    static HRESULT GetParameterDescription( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [in] */ ULONG in_iParameter,
        /* [in] */ ULONG in_LocaleID,
        /* [out] */ DMString *io_pstrDescription);
    
    static HRESULT GetParameterDefaultValue( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [in] */ ULONG in_iParameter,
        /* [out] */ DMString *io_pstrDefaultValue);
    
    static HRESULT GetParameterValueEnumeration( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [in] */ ULONG in_iParameter,
        /* [out] */ DMString *io_pstrValueEnumeration);
    
    static HRESULT ParseParameterValue( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [in] */ ULONG in_iParameter,
        /* [in] */ DMString *in_pstrParameterStrValue,
        /* [in] */ DMVariantPtr io_pParameterMappedValue);
    
    static HRESULT GetMarginalRequirements( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [out] */ DMMarginalRequirements *out_pReq);
    
	static HRESULT GetCaseIDModelled( 
		/* [in] */ IDMContextServices *in_pContext,
		/* [out] */ BOOL *out_pfIsModelled);

    static HRESULT ValidateAttributeSet( 
		/* [in]  */	IDMContextServices* in_pContext,
        /* [in] */ IDMAttributeSet *in_pAttributeSet);

	static HRESULT GetSupportsPMML(
            /* [in] */ IDMContextServices *in_pContext,
            /* [out] */ BOOL *out_pfSupportsPMML);

};

OBJECT_ENTRY_AUTO(__uuidof(ALGORITHM), ALGORITHM)
